from flask import Blueprint, request, jsonify
import json
import logging
from collections import OrderedDict
from utils.utils import build_fail_response, confirm_params_type
from database.db_tools import SQLServerDatabase, connection_string
# 创建蓝图
ts_recheck_erp_res_page = Blueprint('ts_recheck_erp_res_page', __name__)




# 将调用的erp信息存入数据库
@ts_recheck_erp_res_page.route('/pyapi/saveErpRes', methods=['POST'])
def saveErpRes():
    try:
        # 获取参数对象
        data = OrderedDict(sorted(request.json.items()))
        columns = []
        values = []
        # 封装key和value
        for key, value in data.items():
            columns.append(key)
            values.append(value)
        # 操作数据库
        with SQLServerDatabase(connection_string) as db:
            res = db.execute_insert("ts_recheck_erp_res", values=values, columns=columns)
            return jsonify(res)
    except Exception as e:
        return build_fail_response(message=str(e), params=confirm_params_type(request))


